package com.design.movieticket.controller;



import cn.hutool.json.JSONObject;

import com.alibaba.fastjson.JSONArray;
import com.design.movieticket.common.base.constant.Result;
import com.design.movieticket.common.base.constant.ResultStatus;
import com.design.movieticket.entity.Seat;
import com.design.movieticket.service.SeatService;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Api(tags = "座位模块")
@RestController
@RequestMapping("/seat")
public class SeatController {
    @Resource
    private SeatService seatService;

    @GetMapping("/getSeatList")
    public Result getSeatList(Long movieHallId){
        Seat[][] seatList = seatService.getSeatList(movieHallId);
        System.out.println(seatList);
        return new Result(seatList,ResultStatus.REQUEST_SUCCESS.getMsg(),ResultStatus.REQUEST_SUCCESS.getCode());
    }

    @PostMapping("/saveSeat")
    public Result saveSeat(@RequestBody String seatList){
        JSONArray need = JSONArray.parseArray(seatList);

        ObjectMapper objectMapper = new ObjectMapper();
        List<Seat> seat = new ArrayList<>();
        for (int i = 0;i<need.size();i++){
            for (int j =0;j<need.getJSONArray(i).size();j++){
                Object object = need.getJSONArray(i).get(j);
                seat.add(objectMapper.convertValue(object,Seat.class));

            }
        }
        seatService.addOrUpdateSeatList(seat);
        return new Result(ResultStatus.SAVE_SUCCESS.getMsg(),ResultStatus.SAVE_SUCCESS.getCode());
    }






    @GetMapping("/wxGetSeatList")
    public Result wxGetSeatList(Long movieArrangeId){
        return new Result(seatService.wxGetSeatList(movieArrangeId),ResultStatus.REQUEST_SUCCESS.getMsg(),ResultStatus.REQUEST_SUCCESS.getCode());
    }
}
